package capricornus.actor.controller
{
	import capricornus.actor.Actor;
	import capricornus.actor.events.ActorAttributeEvent;
	import capricornus.components.label.Label;
	import capricornus.components.tilelist.SimpleList;

	/**
	 * 负责显示人上方的名字，状态等 
	 * @author kael
	 * 
	 */	
	public class ActorComponentController
	{
		protected var _actor:Actor;
		
		protected var _list:SimpleList;
		protected var _lbName:Label;

		public function ActorComponentController( actor:Actor )
		{
			_actor = actor;
			init();
		}
		
		private function init():void
		{
			_list = new SimpleList();
			_list.mouseChildren = false;
			_list.mouseEnabled = false;
			_list.cacheAsBitmap = true;
			_actor.componentLayer.addChild( _list );
			
			_lbName = new Label();
			_lbName.cacheAsBitmap = true;
			setUserName();
			setUserNameColor();
			_list.addItem( _lbName );
			
			_actor.attribute.addEventListener( ActorAttributeEvent.NAME_CHANGED, onNameChange, false, 0, true );
			_actor.attribute.addEventListener( ActorAttributeEvent.HEIGHT_CHANGE, onHeightChange, false, 0, true );
			
			height = 120;
		}
		
		public function destroy():void
		{
			_actor.attribute.removeEventListener( ActorAttributeEvent.NAME_CHANGED, onNameChange );
		}
		
		public function set height( value:int ):void
		{
			_list.y = -Math.abs(value);
		}
		
		private function onNameChange( e:ActorAttributeEvent ):void
		{
			setUserName();
		}
		
		private function onHeightChange( e:ActorAttributeEvent ):void
		{
			height = _actor.attribute.height;
		}
		
		protected function update():void
		{
			_list.refresh();
		}
		
		protected function setUserName():void
		{
			_lbName.text = _actor.attribute.name;
			_lbName.x = -int(_lbName.width/2);
			update();
		}
		
		protected function setUserNameColor():void
		{
			_lbName.textColor = 0xffffff;
		}
	}
}